<?php
// 热点聚焦
class CURDAction extends Action
{
	// 文章类型
	var $article_types = array();

	//使用的model
	var $model;
	
	//模块名称,用于显示在列表页面
	var $module_name;
	
	//列表显示页面模板
	var $list_template = "Index/content_list";

	//列表排序方式
	var $list_order = "id desc";

	//编辑页面使用模板
	var $edit_template = "Index/edit";

	//是否需要上传文章附件
	var $allow_upload = false;

	//上传文件保存目录
	var $savepath = null;

	//允许上传文章类型,空为无限制
	var $allow_filetype = null;

	//要付值给模板的变量
	var $template_values = array();


	//是否生成缩略图
	var $thumb = false;

	//缩略图规格
	var $thumb_height = 400;
	var $thumb_width = 400;

	public function __construct(){
		parent::__construct();
		foreach($this->template_values as $value){
			$this->assign($value,true);
		}
		if(!empty($this->article_types)){
			$this->assign('storageinfo_type',true);
		}
		if($this->allow_upload === true){
			$this->assign("attachment",true);
		}
	}
	
	/**
	 * index 
	 * 显示文章列表
	 * @access public
	 * @return void
	 */
	public function index(){
		$this->model = M($this->model);
		import("ORG.Util.Page");
		$count = $this->model->count();
		$Page = new Page($count,5);
		$show = $Page->show();
		$list = $this->model->limit($Page->firstRow.','.$Page->listRows)->order($this->list_order)->select();
		$this->assign("module_name",$this->module_name);
		$this->assign("list",$list);
		$this->assign("show",$show);
		$this->display($this->list_template);
	}

	/**
	 * _upload 
	 * 处理上传文件
	 * @access public
	 * @return void
	 */
	public function _upload(){
		if($this->allow_upload === true and !empty($_FILES['file']['name'])){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize = 3292200;
			if(!empty($this->allow_filetype)){
				$upload->allowExts = explode(',', $this->allow_filetype);
			}
			if($this->thumb === true){
				$upload->thumb = true;
				$upload->thumbMaxWidth = $this->thumb_width;
				$upload->thumbMaxHeight = $this->thumb_height;
				$upload->thumbPrefix = '';
			}
			$upload->savePath = APP_PATH.$this->savepath;
			$upload->saveRule = uniqid;
			if (!$upload->upload()) {
				$this->error($upload->getErrorMsg());
			} else {
				$uploadList = $upload->getUploadFileInfo();
			}
			$this->model->savename = $_FILES['file']['name'];
			$this->model->savepath = $this->savepath.$uploadList[0]['savename'];
		}
	}
	/**
	 * add 
	 * 添加文章
	 * @access public
	 * @return void
	 */
	public function add(){
		$this->model = M($this->model);
		if($this->model->create()){
			$this->_upload();
			$this->model->publishtime = time();
			$list = $this->model->add();
			$this->assign("jumpUrl","__URL__/index");
			$this->assign("waitSecond","3");
			if($list !== false){
				$this->success("发表成功!");
			}else{
				$this->error("发表失败!请重试!");
			}
		}
		if($this->allow_upload === true){
			$this->assign("attachment",true);
		}
		$this->assign('allow_filetype',$this->allow_filetype);
		$this->assign("article_types",$this->article_types);
		$this->display($this->edit_template);
	}

	/**
	 * edit 
	 * 编辑文章
	 * @access public
	 * @return void
	 */
	public function edit(){
		$this->model = M($this->model);
		if(!empty($_GET['id'])){
			$vo = $this->model->GetById($_GET['id']);
			if($vo){
				$this->assign("vo",$vo);
				$this->assign("article_types",$this->article_types);
				$this->display($this->edit_template);
			}else{
				$this->error("编辑项不存在");
			}
		}else if(!empty($_POST['id'])){
			if($this->model->create()){
				$this->_upload();
				$list = $this->model->save();
				$this->assign("jumpUrl","__URL__/index");
				$this->assign("waitSecond","3");
				if($list !== false){
					$this->success("修改成功!");
				}else{
					$this->error("修改失败!请重试!");
				}
			}
		}else{
			$this->error("编辑项不存在");
		}
	}

	/**
	 * delete 
	 * 删除文章
	 * @param mixed $id 要删除文章的id
	 * @access public
	 * @return void
	 */
	public function delete(){
		if(!empty($_GET['id'])){
			$this->model = M($this->model);
			$list = $this->model->getById($_GET['id']);
			$result = $this->model->delete($_GET['id']);
			$this->assign("jumpUrl","__URL__/index");
			$this->assign("waitSecond","3");
			if(false != $result){
				if($this->allow_upload === true){
					unlink(APP_PATH.$this->savepath.$list['savename']);
				}
				$this->success("删除成功!");
			}else{
				$this->error("删除失败!请重试!");
			}
		}else{
			$this->error("编辑项不存在");
		}
	}
	/**
	 * publish 
	 * 发布文章
	 * @param mixed $id 要发布文章的id
	 * @access public
	 * @return void
	 */
	public function publish(){
		if(!empty($_GET['id'])){
			$this->model = M($this->model);
			$data['state'] = 1;
			$this->model->where("id={$_GET['id']}")->save($data);
		}else{
			$this->error("编辑项不存在");
		}
		$this->assign("jumpUrl","__URL__/index");
		$this->success('该信息更改为发布');
	}
	/**
	 * unpublish 
	 * 不发布文章
	 * @param mixed $id 要不发布文章的id
	 * @access public
	 * @return void
	 */
	public function unpublish(){
		if(!empty($_GET['id'])){
			$this->model = M($this->model);
			$data['state'] = 0;
			$this->model->where("id={$_GET['id']}")->save($data);
		}else{
			$this->error("编辑项不存在");
		}
		$this->assign("jumpUrl","__URL__/index");
		$this->success('该信息更改为不发布');
	}
}
?>
